﻿<div>
  <input @bind="TextValue"
         disabled="@(!Property.CanWrite)"
         format-value="MM/dd/yyyy" /><br />
  <span class="text-danger">@Property.ErrorText</span>
  <span class="text-warning">@Property.WarningText</span>
  <span class="text-info">@Property.InformationText</span>
</div>

@code {
  [Parameter]
  public Csla.Blazor.IPropertyInfo Property { get; set; }
  [Parameter]
  public bool EmptyIsMin { get; set; } = true;
  [Parameter]
  public string FormatString { get; set; } = "MM/dd/yyyy";

  private Csla.SmartDate DateValue;

  private string TextValue
  {
    get
    {
      DateValue = new Csla.SmartDate((DateTime?)Property.Value, EmptyIsMin);
      DateValue.FormatString = FormatString;
      return DateValue.Text;
    }
    set
    {
      try
      {
        DateValue.Text = value;
      }
      catch (ArgumentException)
      { /* invalid text entry, don't update value */ }
      if (DateValue.IsEmpty)
        Property.Value = null;
      else
        Property.Value = DateValue.Date;
    }
  }
}
